package com.desire.homework;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Objects;

/**
 * @author desire
 */
public class _33_统计有多少个java文件 {
    public static long fileNum = 0;

    public static void main(String[] args) {
        long javaDirNums = getJavaDirNums("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase", "java");
        System.out.println(javaDirNums);
    }

    public static long getJavaDirNums(String director, String suffix) {
        return getJavaDirNums(new File(director), suffix);
    }

    public static long getJavaDirNums(File director, String suffix) {
        if (director.exists()) {
            for (File file : Objects.requireNonNull(director.listFiles())) {
                if (file.isDirectory()) {
                    getJavaDirNums(file, suffix);
                } else if (file.getName().toLowerCase().endsWith(suffix)) {
                    fileNum++;
                }
            }
        } else {
            try {
                throw new FileNotFoundException("目录不存在!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return fileNum;
    }
}
